Trend
The first step is to detect the current trend, i.e., bullish or bearish or sideways market. Buy signals will always be generated at the bottom of a bearish trend and sell signals at the peak of a bullish trend.
For this purpose, I used a modified version of the SuperTrend indicator using Pivots.
SuperTrend: This involves drawing a center line, typically a moving average but in this modified version as a weighted average of the moving average and high/low pivots, and using ATR - a measure of volatility - to draw upper and lower bands on the center line.
Whenever the price crosses over the upper band, it is a bullish trend. Similarly, whenever the price moves below the lower band, it is a bearish trend.
In the modified version, whenever the price is between the two bands, it is a sideways market.
The Trend is shifted two days forward to make it clear that buy/sell signals are always generated within bearish/bullish trends respectively.